//===================================
//File: Placement.cpp
//Placement functions
//Name: Smirnova O.P.
//Date: 28.06.12, v.1.0
//===================================
#include "Place.h"
unsigned int Placement::count=0;
unsigned int Placement::count_all=0;
using namespace std;
//---------------------------------------------------------------------------
//Конструктор с параметрами
Placement::Placement(Point tl,int w,int h):id(++count_all), Shelf(tl,w,h)
  { count++;
	 
	 /* cout << "Placement:     \n";
 
    cout << "id = " << id_w << " / count = " << count_w << " / count_all = " << count_all_w
	    << "  Placement.\n";
	getch();
  */
  };  
//---------------------------------------------------------------------------  
//"Говорящий" деструктор
  Placement::~Placement(){
	  count--;
   /* cout << "~Placement: \n";
    cout <<"id="<<id 
         <<" count_all="<<count_all
         <<" count=" << count << endl 
         << "~Placement.\n";
    getch();
	*/
  };  
//---------------------------------------------------------------------------
//Перещает полку на dxdy
  void Placement::moveShelfTodXdY(const double dx, const double dy)
  {
	  Shelf::moveTodXdY(dx, dy);
  };  
//---------------------------------------------------------------------------
//Перемещает полку в точку
  void Placement::moveShelfToPoint(const Point P)
  {
	  Shelf::moveTo(P);
  };    
//---------------------------------------------------------------------------
//Перемещает все изображения на dxdy
  void Placement::moveImages(const double dx, const double dy)
  {
	  if(Collection::getCollection()->isEmpty()==0)
	  {
	  Collection::moveTodXdY(dx, dy);
	  MultiListElem<Image>* temp = getCollection()->getRootListA();
			 
	while (temp != 0)
	{
		Image s1 = temp->getValue();
		if (isVisible(s1)==1)
		{temp=temp->getElema();
		continue;}
		else {
			cout<<"Perfect_Image with ID= "<<s1.getID()<<" out of shelf.It moved back Collection"<<endl;
			temp=temp->getElema();
			s1.moveTodXdY(-dx,-dy);
            temp->setValue(s1);
            }
	}
	  }
	  else cout << "Collection is Empty" << endl;
  };    
//---------------------------------------------------------------------------
//Перемещает полку с изображениями на dxdy
  void Placement::moveAll(const double dx, const double dy)
  {
	  Shelf::moveTodXdY(dx, dy);
	  if(Collection::getCollection()->isEmpty()==0)
	  {
	  Collection::moveTodXdY(dx, dy);
	  	  MultiListElem<Image>* temp = getCollection()->getRootListA();
			 
	while (temp != 0)
	{
		Image s1 = temp->getValue();
		if (isVisible(s1)==1)
		{temp=temp->getElema();
		continue;}
		else {
			cout<<"Perfect_Image with ID= "<<s1.getID()<<" out of shelf.It moved back Collection"<<endl;
			temp=temp->getElema();
			s1.moveTodXdY(-dx,-dy);
            temp->setValue(s1);
            }
	}
	  }
	  else cout << "Collection is Empty" << endl;
  };      
//---------------------------------------------------------------------------
//Проверяет, не выходит ли изображение за границы полки
  short Placement::isVisible(const Image I)
  {
	  short flg=1;
	  Elem<Point> *iter = I.getPoints()->getHead();
  while ((iter!= NULL) && flg==1) 
  {
	  
	  if (isVisibleP(iter->getValue())==0) 
		flg=0;

	  iter = iter->getNext();
  }
  return flg;
  };      
//---------------------------------------------------------------------------  
//Проверяет, не выходит ли точка за границы полки
  short Placement::isVisibleP(Point p)
  {
  short flg = 0;
  double x=p.getX();
  double y=p.getY();
  double x1=Shelf::getPoint().getX();
  double y1=Shelf::getPoint().getY();
  double x2=Shelf::getPoint().getX()+Shelf::getWidth();
  double y2=Shelf::getPoint().getY()+Shelf::getHeight();
  if ((x>x1) && (x<x2) && (y>y1) &&(y<y2)) flg = 1;
  return flg;
  };
  //---------------------------------------------------------------------------

//---------------------------------------------------------------------------
  const unsigned int Placement::getID()
{
  return id;
};

  unsigned int Placement::countObject()
{
  return count; 
};
